# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=klfc
pkgver=1.5.7
pkgrel=0
pkgdesc="Keyboard Layout Files Creator"
url="https://github.com/39aldo39/klfc"
arch="x86_64 aarch64"  # limited by ghc / cabal
license="GPL-3.0"
makedepends="cabal ghc"
subpackages="$pkgname-doc"
source="https://github.com/39aldo39/klfc/archive/v$pkgver/klfc-$pkgver.tar.gz
	ghc9-001.patch
	ghc9-002.patch
	cabal.config
	"
options="!check"  # it seems that no tests are provided

# Cabal seems to be built without sandbox, moving the cabal-dir into src.
export CABAL_DIR="$srcdir/.cabal"

cabal_update() {
	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.config with version constraints.
	cabal update
	(
		cd "$builddir"
		cabal v1-freeze --shadow-installed-packages

		# Add version tag at the first line.
		sed -i "1i--$pkgver" cabal.config

		mv cabal.config "$startdir"/
	)

	cd "$startdir"
	abuild checksum || die "Failed to update checksum, run 'abuild checksum' manually"
}

prepare() {
	default_prepare

	if [ "$(head -n 1 "$srcdir"/cabal.config)" != "--$pkgver" ]; then
		die "Requirements file is outdated, run 'abuild cabal_update'"
	fi
	ln -sf "$srcdir"/cabal.config "$builddir"/cabal.config
}

build() {
	cabal update
	cabal install --only-dependencies
	cabal configure \
		--prefix='/usr' \
		--bindir='$prefix/bin' \
		--sysconfdir='/etc' \
		--enable-executable-stripping \
		--enable-split-sections \
		--offline \
		--jobs=${JOBS:-2} \
		--ghc-options="${CFLAGS:+$(echo " $CFLAGS" | sed 's/ / -optc/g')}" \
		--ghc-options="${LDFLAGS:+$(echo " $LDFLAGS" | sed 's/ / -optl/g')}" \
		--ghc-option='-split-sections'
	cabal build
}

check() {
	cabal test
}

package() {
	local docdir="$pkgdir/usr/share/doc/$pkgname"

	install -D -m755 dist-newstyle/build/*/*/*/build/klfc/klfc \
		"$pkgdir"/usr/bin/klfc

	mkdir -p "$docdir"
	cp -r doc/* "$docdir"/
	cp -r examples "$docdir"/
}

sha512sums="
bae6c7b3c21419e9fea73b38eaea7c7c27b9cb78b5bccd7ce11e733729f37d8d0b8d20435fa63bfb3c5be6479cca0caccc63d59a99aa04477414eddf9ce20919  klfc-1.5.7.tar.gz
6acf2cb52be8b22bf8d40186bbea8a211f7237b808d72321d077722d33f9fb5f3da5f7f2ed345b2a8631046c31ed0feaeec3cb4ea24f68439d2af48f366e2a1b  ghc9-001.patch
febe8b7ce38393f8f0576cfd77c4eca56864c05c9028c2f94cf008f73eeca753c6247a71f03728e8f650e612193cf737b95b9ad3fbc96edc251f8fedc3cfe8ce  ghc9-002.patch
5be1e6dbfe1849fb5b7c01ada95ccdbf0c769463bac62dc49732fed8346e6b32bab2a52dadafc9e8cb7608122d85e4bbda4c1dd967a606902e79357e2571fba7  cabal.config
"
